#coding=utf-8
# 2022.6.4
# 2022.6.24 修改了排序对象和排序参考对象
# 2022.6.25 注释debug
import hashlib
from SqlDB import myDB
import time
import os
import random
sourcepath='source'
learndb=myDB('learn.db')
learndb.creat_table('source', createdict={"mid": "INTEGER PRIMARY KEY  AUTOINCREMENT","path": "TEXT NOT NULL","md5": "TEXT","lasttime":"INTEGER","learncount":"FLOAT","addtime":"CHARACTER(32)"})

def getmids():
    mds=learndb.row_query("SELECT mid FROM source")
    for md in mds:
        yield md[0]
#littlefile
def get_fileMD5(filepath):
    if os.path.isfile(filepath):
        f=open(filepath,'rb')
        md5obj = hashlib.md5()
        md5obj.update(f.read())
        hash = md5obj.hexdigest()
        f.close()
        return str(hash).upper()
    return None

def get_stuname():
    current_time=str(time.time())
    currentstr=current_time.replace(".",'')
    newside=random.sample('zyxwvutsrqponmlkjihgfedcba',7)
    side=""
    for i in newside:
        side+=i
    return currentstr+'_('+side+')',currentstr
#huge file
def getBigFileMD5(filepath):
    if os.path.isfile(filepath):
        md5obj = hashlib.md5()
        maxbuf = 8192
        f = open(filepath,'rb')
        while True:
            buf = f.read(maxbuf)
            if not buf:
                break
            md5obj.update(buf)
        f.close()
        hash = md5obj.hexdigest()
        return str(hash).upper()
    return None

#已经弃用，使用更精确的6.24版
def std_filelist(filelist):
    d={}
    for file in filelist:
        filepath=os.path.join(sourcepath,file)
        p=time.ctime(os.path.getmtime(filepath))
        d[file]=p
    m=sorted(d.items(),key=lambda x:x[1],reverse=False)
    for i in m:
        yield i[0]

#6.24版文件排序器
def std_filelist_bymTime(filelist):
    d=[]
    for file in filelist:
        filepath=os.path.join(sourcepath,file)
        mt=os.path.getmtime(filepath)
        ct=os.path.getctime(filepath)
        pmt=time.ctime(mt)
        cmt=time.ctime(ct)
        # print(filepath,"\t",ct,"\t",mt,"\t",pmt,"\t",cmt)
        d.append((file,mt))
    d.sort(key=lambda tup:tup[1])
    return d
if __name__=='__main__':
    filelist=os.listdir(sourcepath)
    totual_num=len(filelist)
    existmids=[]
    newaddlist=[]
    for file in filelist:
        exten=file.split(".")[-1]
        filepath=os.path.join(sourcepath,file)
        md5=get_fileMD5(filepath)
        k=learndb.search_one('source', ['mid'], ['md5'], (md5,))
        if k:
            existmids.append(k[0])
            continue
        else:
            newaddlist.append(file)
    stdnewflist=std_filelist_bymTime(newaddlist)
    for file_time in stdnewflist:
        file=file_time[0]
        exten=file.split(".")[-1]
        filepath=os.path.join(sourcepath,file)
        newname=""
        current_timestr=""
        while True:
            nfn,current_timestr=get_stuname()
            newname="{}.{}".format(nfn,exten)
            newpath=os.path.join(sourcepath,newname)
            if os.path.exists(newpath):
                continue
            else:
                break
        os.rename(filepath,newpath)
        md5=get_fileMD5(newpath)
        newid=learndb.insert('source', ['path','md5','learncount','addtime'], (newname,md5,0,current_timestr))
        existmids.append(newid)
        print("新加入{}".format(newid))
    dellist=[]
    for mid in getmids():
        if mid in existmids:
            print('存在{}'.format(mid))
        else:
            print('计划删除{}'.format(mid))
            dellist.append(mid)
    print("开始删除")
    for delmid in dellist:
        learndb.delete('source', ['mid'], (delmid,))
        print('已经删除{}'.format(delmid))
